module fsm_flowing_LED (
	input  wire clk,
	input  wire resetn,

	output wire LED1,
	output wire LED2,
	output wire LED3
);
	
/*
 *计时部分
 */

reg [0:7] cnt;

//计时最大值
parameter MAX_CNT = 49;

always @(posedge clk or negedge resetn) begin
	if(!resetn)
	begin
		cnt <= 8'b00_000_000;
	end
	else if(cnt == MAX_CNT)
	begin
		cnt <= 8'b00_000_000;
	end
	else
	begin
		cnt <= cnt+1;
	end
end

/*
 *状态机部分
 */
parameter LED1_ON = 3'b001;
parameter LED2_ON = 3'b010;
parameter LED3_ON = 3'b100;

reg [0:2] current_state;
reg [0:2] next_state;

//状态转移
always @(posedge clk or negedge resetn) begin
	if(!resetn)
	begin
		current_state <= LED1_ON;
	end
	else
	begin
		current_state <= next_state;
	end
end

//状态跳转条件
always @(*) begin
	if(!resetn)
	begin
		next_state = LED1_ON;
	end
	else
	begin
		case (current_state)
			LED1_ON:
			begin
				if(cnt == MAX_CNT)next_state <= LED2_ON;
				else next_state <= next_state;
			end
			LED2_ON:
			begin
				if(cnt == MAX_CNT)next_state <= LED3_ON;
				else next_state <= next_state;
			end
			LED3_ON:
			begin
				if(cnt == MAX_CNT)next_state <= LED1_ON;
				else next_state <= next_state;
			end
			default:
			begin
				next_state <= LED1_ON;
			end
		endcase
	end
end

//状态输出
assign LED1 = {current_state == LED1_ON};
assign LED2 = {current_state == LED2_ON};
assign LED3 = {current_state == LED3_ON};

endmodule